#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查数据库中所有医生的详细信息
"""
import pymysql

# 数据库配置
DB_CONFIG = {
    'host': 'localhost',
    'user': 'flask_dev',
    'password': 'dev123456',
    'database': 'flask_local',
    'charset': 'utf8mb4'
}

def check_all_doctors():
    """检查所有医生信息"""
    try:
        conn = pymysql.connect(**DB_CONFIG)
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        
        # 查询所有医生用户及其详细信息
        print("=" * 100)
        print("查询所有医生用户及详细信息")
        print("=" * 100)
        
        cursor.execute("""
            SELECT 
                u.id as user_id,
                u.openid,
                u.wechat_name,
                u.phone,
                u.role,
                u.status,
                u.create_time as user_create_time,
                d.id as doctor_detail_id,
                d.real_name,
                d.hospital,
                d.department,
                d.position,
                d.create_time as detail_create_time
            FROM user u
            LEFT JOIN user_doctor d ON u.id = d.user_id
            WHERE u.role = 1
            ORDER BY u.id DESC
        """)
        doctors = cursor.fetchall()
        
        if not doctors:
            print("[WARNING] 没有找到医生用户")
            return
        
        print(f"\n找到 {len(doctors)} 个医生用户:\n")
        
        for i, doc in enumerate(doctors, 1):
            print(f"\n{'='*80}")
            print(f"医生 #{i}:")
            print(f"{'='*80}")
            print(f"【基础信息】")
            print(f"  用户ID: {doc['user_id']}")
            print(f"  OpenID: {doc['openid']}")
            print(f"  微信昵称: {doc['wechat_name']}")
            print(f"  手机号: {doc['phone']}")
            print(f"  账号状态: {'正常' if doc['status'] == 1 else '禁用'}")
            print(f"  注册时间: {doc['user_create_time']}")
            
            if doc['doctor_detail_id']:
                print(f"\n【详细信息】")
                print(f"  真实姓名: {doc['real_name']}")
                print(f"  医院: {doc['hospital']}")
                print(f"  科室: {doc['department']}")
                print(f"  职位: {doc['position']}")
                print(f"  完善时间: {doc['detail_create_time']}")
            else:
                print(f"\n【详细信息】")
                print(f"  [未完善] 该医生还未填写详细信息")
        
        print(f"\n{'='*100}")
        print("查询完成！")
        print(f"{'='*100}")
        
        # 查找肖智原
        cursor.execute("""
            SELECT u.id, u.phone, d.real_name 
            FROM user u
            LEFT JOIN user_doctor d ON u.id = d.user_id
            WHERE u.role = 1 AND d.real_name LIKE '%肖%'
        """)
        xiao_doctors = cursor.fetchall()
        
        if xiao_doctors:
            print(f"\n[INFO] 找到姓'肖'的医生:")
            for doc in xiao_doctors:
                print(f"  用户ID: {doc['id']}, 姓名: {doc['real_name']}, 手机: {doc['phone']}")
        else:
            print(f"\n[WARNING] 未找到姓名包含'肖'的医生")
        
        cursor.close()
        conn.close()
        
    except pymysql.Error as e:
        print(f"[ERROR] 数据库错误: {e}")
    except Exception as e:
        print(f"[ERROR] 执行失败: {e}")

if __name__ == '__main__':
    check_all_doctors()


